<?php

/**
 * Description of FamilyMember
 *
 * @author Admin
 */
class Family_Resource_Familymember extends WOW_Model_Resource_Db_Table_Abstract implements
Family_Resource_Familymember_Interface
{
    protected $_name = 'family';
    protected $_primary = 'id';
    protected $_rowClass = 'Family_Resource_Familymember_Item';


    public function getRowById($id)
    {
        return $this->find($id)->current();
    }

    /**
     * Finds data by joining on three tables, family, relations, family_member_info
     *
     * @param integer $id
     * @return Family_Model_Familymember
     */
    public function getMemberById($id)
    {
         $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('family', array('id', 'userid', 'relation_id', 'fmid', 'isRoot', 'parent_id', 'family_lineage'))
                        ->join('relations', 'family.relation_id = relations.id', array('RelationName'))
                        ->join('family_member_info', 'family_member_info.id = family.fmid', array('FirstName', 'MiddleName', 'LastName'))
                        ->where('family.id = ?', $id);

        return $this->fetchAll($select);
    }

    /**
        * Finds data by joining on three tables, family, relations, family_member_info based on a given user
     * 
     */
    public function getWholeFamily($userid)
    {
        $select = $this->select()
                        ->setIntegrityCheck(false)
                        ->from('family', array('id', 'userid', 'relation_id', 'fmid', 'isRoot', 'parent_id', 'family_lineage'))
                        ->join('relations', 'family.relation_id = relations.id', array('RelationName'))
                        ->join('family_member_info', 'family_member_info.id = family.fmid', array('FirstName', 'MiddleName', 'LastName'))
                        ->where('userid = ?', $userid);

        return $this->fetchAll($select);
    }

   /**
        * Finds data of family table
    */
    public function getUserFamilyTableData($userid)
    {
         $select = $this->select()->where('userid = ?', $userid)
                                    ->where('isRoot = ?', 1);
         
         $result = $this->fetchAll($select);

         foreach($result as $temp)
         {
             $family_user_id = $temp->id;
         }

         $sel = $this->select()->where('parent_id = ?', $family_user_id );
         return $this->fetchAll($sel);
    }

}
?>
